package com.fourpigeon.domain;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fourpigeon.utils.COSUtils;
import com.fourpigeon.utils.IdGenerator;
import com.fourpigeon.utils.LocalDateTimeSerializerUtils;
import com.qcloud.cos.http.HttpMethodName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
//
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BlogAgree{
    /**
     * 主键
     */
    private Long id;
    /**
     * 用户id
     */
    private Long userId;
    /**
     * 用户名称
     */
    private String userName;
    /**
     * 用户头像
     */
    private String icon;
    /**
     * 球局名称
     */
    private String name;
    /**
     * 球局时间
     */
    private String time;
    /**
     * 地点
     */
    private String location;
    /**
     * 球局类型
     */
    private Short type;
    /**
     * 当前人数
     */
    private Short curpeople = 1;
    /**
     * 球局人数
     */
    private String people;
    /**
     * 联系方式
     */
    private String contact;
    /**
     * 补充内容
     */
    private String content;
    /**
     * 状态码：0正常，1：被举报，2：禁止查看
     */
    private Integer status = 0;
    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime updateTime;

    /**
     * 头像url
     */
    private String iconUrl = "";

    public void set(Long id,User user){
        this.id = id;
        this.userId = user.getId();
        this.userName = user.getName();
        this.icon = user.getIcon();
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    /**
     * 比较并更新，
     * 如果没有不同的变量，直接返回false，
     * 否则true。
     * @param blogAgreeDTO
     * @return
     */
    public BlogAgree matchAndUpdate(BlogAgreeDTO blogAgreeDTO){
        //结果列表，用于返回不同的元素的key
        List<Object> resultList = new ArrayList<>();
        Map<String, Object> oriMap = BeanUtil.beanToMap(this);
        Map<String, Object> newMap = BeanUtil.beanToMap(blogAgreeDTO);
        for (String key : newMap.keySet()) {
            if (key.equals("iconUrl")) continue;
            //不相同，更新map中的数据
            if (!newMap.get(key).equals(oriMap.get(key))){
                oriMap.remove(key);
                oriMap.put(key,newMap.get(key));
                resultList.add(key);
            }
        }
        if (resultList.isEmpty()) {
            return null;
        }
        //mapToBean
        BlogAgree temp = BeanUtil.mapToBean(oriMap, BlogAgree.class, false);
        return temp;
    }

    public BlogAgree responseType(){
        this.status = null;
        this.createTime = null;
        this.updateTime = null;
        return this;
    }

    public void handlerImgs() {
        this.iconUrl = COSUtils.getSign(this.icon, HttpMethodName.GET);
    }
}


